package org.example.MQ.rabbitmq.WorkQueue;

import com.rabbitmq.client.*;
import org.example.MQ.rabbitmq.config.ConnectionUtil;

import java.io.IOException;

/**
 * @Author : zhangbo
 * @data : 2024/4/12
 * @description :
 */
public class ConsumerWork2 {
    /**
     * 队列名称
     */
    private static final String QUEUE_NAME = "workQueue";

    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        try {
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            //创建消费者对象
            MyConsumer consumer = new MyConsumer(channel);
            channel.basicConsume(QUEUE_NAME, true, consumer);
            //手动 ack
            //channel.basicConsume(QUEUE_NAME, false, consumer);
            System.out.println("消费者2监听队列。。。。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //自定义消息消费者
    static class MyConsumer extends DefaultConsumer {
        public MyConsumer(Channel channel){
            super(channel);
        }
        //重写handleDelivery方法
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            System.out.println("消费者2消费" + new String(body, "UTF-8"));
            //手动ack
            //this.getChannel().basicAck(envelope.getDeliveryTag(), false);
        }
    }

}
